/**
 * 常用 操作
 */

import store from '../store/index'

import base64 from './base64'
import Fav from '../api/fav'

import request from './request'
const model = new request()

const favModel = new Fav()

// 保存推荐人
export function saveReferrer(options) {
  if (options.scene && (options.scene > 0 || options.scene.length > 0)) {

    console.log('referrer', options.scene)

    model.setReferrer(options.scene)
  }
}

/* 判断用户登录没有 */
export function _isLogin() {
  return new Promise((resolve, reject) => {
    const token = store.getters['User/token']
    if (!token) {
      reject()
    } else {
      favModel.isLogin({ token }).then(response => {
        let { data } = response
        data = base64.parse(data)
        if (data.isLogin) {
          resolve()
        } else {
          // 没有登录
          reject()
        }
      })
    }
  })
}

// 点击导航
export function goTo(item) {
  // console.log(item.url)
  if (!item.url) {
    return
  }
  if (item.url.indexOf('/pages/index/index') !== -1 && item.url.indexOf('?type=') !== -1) {
    store.dispatch('Url/set_url', item.url)
  } else {
    uni.navigateTo({
      url: item.url
    })
  }
}


